package nosqlconnect.views;


import java.util.Iterator;

import nosqlconnect.connection.Connection;
import nosqlconnect.connection.providers.ConnectionContentProvider;
import nosqlconnect.connection.providers.ConnectionLabelProvider;
import nosqlconnect.connection.providers.ConnectionTableLabelProvider;
import nosqlconnect.menuActions.MConnectAction;
import nosqlconnect.menuActions.MDeleteConnectionAction;
import nosqlconnect.menuActions.MEditConnectionAction;
import nosqlconnect.toolbarActions.TDeleteConnectionAction;
import nosqlconnect.toolbarActions.TEditConnectionAction;
import nosqlconnect.toolbarActions.TNewConnectionAction;
import nosqlconnect.Activator;

import org.eclipse.jface.action.IToolBarManager;
import org.eclipse.jface.action.MenuManager;
import org.eclipse.jface.action.Separator;
import org.eclipse.jface.viewers.ISelection;
import org.eclipse.jface.viewers.ISelectionChangedListener;
import org.eclipse.jface.viewers.IStructuredSelection;
import org.eclipse.jface.viewers.ListViewer;
import org.eclipse.jface.viewers.SelectionChangedEvent;
import org.eclipse.jface.viewers.TableViewer;
import org.eclipse.swt.SWT;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Menu;
import org.eclipse.swt.widgets.Table;
import org.eclipse.swt.widgets.TableColumn;
import org.eclipse.ui.part.ViewPart;

public class ConnectionsView extends ViewPart {

	private ListViewer connectionList;
	private TableViewer tableViewer;
	private MenuManager menuMgr;
	//declare actions
	private MConnectAction mConnectAction = new MConnectAction();
	private MEditConnectionAction mEditAction = new MEditConnectionAction();
	private MDeleteConnectionAction mDeleteAction = new MDeleteConnectionAction();
	
	public ConnectionsView() {
	}

	@Override
	public void createPartControl(Composite parent) {
		createConnectionTable(parent);
		addActionsToToolbar();
		addContextMenu(tableViewer);
		handleTableSelectionChanged();
	}

	@Override
	public void setFocus() {

	}

	private void addActionsToToolbar(){
		IToolBarManager toolBarMgr = getViewSite().getActionBars().getToolBarManager();
		toolBarMgr.add(new TNewConnectionAction());
		toolBarMgr.add(new TDeleteConnectionAction());
		toolBarMgr.add(new TEditConnectionAction());
	}
	
	private void addContextMenu(TableViewer tableViewer){
		menuMgr = new MenuManager();
        Menu menu = menuMgr.createContextMenu(tableViewer.getControl());
        tableViewer.getControl().setMenu(menu);
        getSite().registerContextMenu(menuMgr, tableViewer);
        menuMgr.add(mConnectAction);
        menuMgr.add(mEditAction);
        menuMgr.add(mDeleteAction);
	}
	
	private void initConnectionList(){
		connectionList.setContentProvider(new ConnectionContentProvider());
		connectionList.setLabelProvider(new ConnectionLabelProvider());
		//connectionList.setInput();

		//connectionList.addSelectionChangedListener(mConnectAction);
	}
	
	private void createConnectionTable(Composite parent){
		tableViewer = new TableViewer(parent,SWT.SINGLE | SWT.FULL_SELECTION);
		Table table = tableViewer.getTable();
		table.setHeaderVisible(true);

		String[] columnNames = new String[]{"Name", "Status"};
		
		for (int i = 0; i < columnNames.length; i++) {
			TableColumn tableColumn = new TableColumn(table,SWT.CENTER);
			tableColumn.setText(columnNames[i]);
			tableColumn.setWidth(100);
		}
		
		tableViewer.setLabelProvider(new ConnectionTableLabelProvider());
		tableViewer.setContentProvider(new ConnectionContentProvider());
		tableViewer.setInput(Activator.getDefault().getConnections());
	}
	
	private void handleTableSelectionChanged(){
		tableViewer.addSelectionChangedListener(
			new ISelectionChangedListener(){
				public void selectionChanged(SelectionChangedEvent event) {
					if(event.getSelection() instanceof IStructuredSelection) {
						IStructuredSelection selection = (IStructuredSelection)event.getSelection();
						
						for (Iterator it = selection.iterator(); it.hasNext();){
							Object connection = it.next();
							if (connection instanceof Connection){
								mConnectAction.setSelectedConnection((Connection)connection);
								mDeleteAction.setSelectedConnection((Connection)connection);
								mEditAction.setSelectedConnection((Connection)connection);
							}
							else {
							}
						}
					}
					
				}
			}
		);
	}
}
